﻿using Dapper;
using Entity;
using IRealize;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Realize
{
    public class Cpc : ICpc
    {
        public async Task<List<Config_Public_Char>> Server(string sqlserver)
        {
            Task<List<Config_Public_Char>> task = Task<List<Config_Public_Char>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from config_public_char";
                    return con.Query<Config_Public_Char>(sql).ToList();
                }
            });
            return await task;
        }
        public async Task<List<Config_Public_Char>> Server2(int id,string sqlserver)
        {
            Task<List<Config_Public_Char>> task = Task<List<Config_Public_Char>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from config_public_char where pbc_id = {id}";
                    return con.Query<Config_Public_Char>(sql).ToList();
                }
            });
            return await task;
        }
        public async Task<int> Delete(int id,string sqlserver) 
        {
            using (SqlConnection con = new SqlConnection(sqlserver)) 
            {
                string sql = $"delete from config_public_char where pbc_id = ${id}";
                return await con.ExecuteAsync(sql);

            }
        }

        public async Task<int> Insert(Config_Public_Char cpc, string sqlserver)
        {
            using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
            {
                string sql = $@"insert into config_public_char(attribute_kind,attribute_name)
                 values('{cpc.attribute_kind}','{cpc.attribute_name}') ";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }
        public async Task<int> Update(int id, string attribute_kind, string attribute_name, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $"update config_public_char set attribute_kind = '{attribute_kind}',attribute_name = '{attribute_name}' WHERE pbc_id = {id}";
                return await con.ExecuteAsync(sql);

            }
        }
        /*public async Task<int> DeleteCFFKAsync(int fkk_id, string sqlserver)
{
   using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
   {

       string sql = $@"delete from [dbo].[config_file_first_kind] where [ffk_id]='{fkk_id}'";
       return await sqlConnection.ExecuteAsync(sql);
   }
}*/
    }
}
